<?php
define(BHUTANESE_REFUGEES_OPTION_NAME, "bhutanese_refugees_option_name");
$bhutanese_settings = get_option(BHUTANESE_REFUGEES_OPTION_NAME);
add_theme_support('post-thumbnails');
add_image_size('bhutanese_refugee_news_large_thumbnail', 300, 300, false);
add_image_size('bhutanese_refugee_news_small_thumbnail', 100, 75, false);
define(BHUTANESE_REFUGEES_MENU_NAME, (isset($bhutanese_settings["main_menu"]) && is_numeric($bhutanese_settings["main_menu"])) ? $bhutanese_settings["main_menu"] : "Main Menu");
define(BHUTANESE_REFUGEES_FOOTER_MENU_NAME, (isset($bhutanese_settings["footer_menu"]) && is_numeric($bhutanese_settings["footer_menu"])) ? $bhutanese_settings["footer_menu"] : "Footer Menu");
define(BHUTANESE_REFUGEES_HOMEPAGE_ID, (isset($bhutanese_settings["homepageId"]) && is_numeric($bhutanese_settings["homepageId"])) ? $bhutanese_settings["homepageId"] : 10);
define(BHUTANESE_REFUGEES_NEWS_ID, (isset($bhutanese_settings["newsId"]) && is_numeric($bhutanese_settings["newsId"])) ? $bhutanese_settings["newsId"] : 4);
define(BHUTANESE_REFUGEES_BLOG_ID, (isset($bhutanese_settings["blogId"]) && is_numeric($bhutanese_settings["blogId"])) ? $bhutanese_settings["blogId"] : 9);

require_once('menu-widget.php');
require_once('category-post-widget.php');

function excerpt($limit) {
    $excerpt = explode(' ', get_the_excerpt(), $limit);
    if (count($excerpt) >= $limit) {
        array_pop($excerpt);
        $excerpt = implode(" ", $excerpt) . '...';
    } else {
        $excerpt = implode(" ", $excerpt);
    }
    $excerpt = preg_replace('`\[[^\]]*\]`', '', $excerpt);
    return $excerpt;
}

function content($limit) {
    $content = explode(' ', get_the_content(), $limit);
    if (count($content) >= $limit) {
        array_pop($content);
        $content = implode(" ", $content) . '...';
    } else {
        $content = implode(" ", $content);
    }
    $content = preg_replace('/\[.+\]/', '', $content);
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]&gt;', $content);
    return $content;
}

function bhutanese_refugees_widgets_init() {
    // Area 1, located at the top of the sidebar.
    register_sidebar(array(
        'name' => __('Primary Widget Area', 'bhutanese_refugees'),
        'id' => 'primary-widget-area',
        'description' => __('The primary widget area', 'bhutanese_refugees'),
        'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h3 class="widget-title orange_bar">',
        'after_title' => '</h3>',
    ));
    register_sidebar(array(
        'name' => __('News Sidebar', 'bhutanese_refugees'),
        'id' => 'news-sidebar-widget-area',
        'description' => __('News sidebar widget area', 'bhutanese_refugees'),
        'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h3 class="widget-title orange_bar">',
        'after_title' => '</h3>',
    ));
    register_sidebar(array(
        'name' => __('Left Sidebar', 'bhutanese_refugees'),
        'id' => 'left-sidebar-widget-area',
        'description' => __('Left menu sidebar widget area', 'bhutanese_refugees'),
        'before_widget' => '<li id="%1$s" class="widget-container  %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h3 class="widget-title blue_bar">',
        'after_title' => '</h3>',
    ));
    register_sidebar(array(
        'name' => __('Footer Links', 'bhutanese_refugees'),
        'id' => 'footer-links-widget-area',
        'description' => __('Footer links widget area', 'bhutanese_refugees'),
        'before_widget' => '<li id="%1$s" class="footer-widget-container %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ));
}

/** Register sidebars by running bhutanese_refugees_widgets_init() on the widgets_init hook. */
add_action('widgets_init', 'bhutanese_refugees_widgets_init');

function bhutanese_refugees_main_nav_tree($menu="") {
    wp_nav_menu(array(
        //'container' => false,                           // remove nav container
        'container_class' => 'menu-tree-wrapper', // class of container (should you choose to use it)
        'menu' => $menu, // nav name
        'menu_class' => 'menu-tree-ul clearfix', // adding custom nav class
        'theme_location' => 'primary', // where it's located in the theme
        'before' => '', // before the menu
        'after' => '', // after the menu
        'link_before' => '', // before each link
        'link_after' => '', // after each link
        //'depth' => 0,                                   // limit the depth of the nav
        'fallback_cb' => 'wp_page_menu'      // fallback function
    ));
}

/* end bhutanese_refugees_main_nav */

function bhutanese_refugees_main_nav($menu="") {
    wp_nav_menu(array(
        //'container' => false,                           // remove nav container
        'container_class' => 'menu', // class of container (should you choose to use it)
        'menu' => $menu, // nav name
        'menu_class' => 'clearfix', // adding custom nav class
        'theme_location' => 'primary', // where it's located in the theme
        'before' => '', // before the menu
        'after' => '', // after the menu
        'link_before' => '', // before each link
        'link_after' => '', // after each link
        //'depth' => 0,                                   // limit the depth of the nav
        'fallback_cb' => 'wp_page_menu'      // fallback function
    ));
}

/* end bhutanese_refugees_main_nav */

// the footer menu (should you choose to use one)
function bhutanese_refugees_footer_links($menu="") {
    // display the wp3 menu if available
    wp_nav_menu(array(
        'container' => '', // remove nav container
        'container_class' => 'footer-links clearfix', // class of container (should you choose to use it)
        'menu' => $menu, // nav name
        'menu_class' => 'footer-nav clearfix right', // adding custom nav class
        'theme_location' => 'footer-links', // where it's located in the theme
        'before' => '', // before the menu
        'after' => '', // after the menu
        'link_before' => '', // before each link
        'link_after' => '', // after each link
        'depth' => 0, // limit the depth of the nav
        'fallback_cb' => 'wp_page_menu'  // fallback function
    ));
}

/* end bones footer link */

function bhutanese_refugees_remove_recent_comments_style() {
    add_filter('show_recent_comments_widget_style', '__return_false');
}

add_action('widgets_init', 'bhutanese_refugees_remove_recent_comments_style');

if (!function_exists('bhutanese_refugees_posted_on')) :

    /**
     * Prints HTML with meta information for the current post-date/time and author.
     *
     * @since Twenty Ten 1.0
     */
    function bhutanese_refugees_posted_on() {
        printf(__('<span class="%1$s"></span> %2$s <span class="meta-sep">by</span> %3$s', 'bhutanese_refugees'), 'meta-prep meta-prep-author', sprintf('<a href="%1$s" title="%2$s" rel="bookmark"><span class="entry-date">%3$s</span></a>', get_permalink(), esc_attr(get_the_time()), get_the_date()
                ), sprintf('<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s">%3$s</a></span>', get_author_posts_url(get_the_author_meta('ID')), esc_attr(sprintf(__('View all posts by %s', 'bhutanese_refugees'), get_the_author())), get_the_author()
                )
        );
    }

endif;

if (!function_exists('bhutanese_refugees_posted_in')) :

    /**
     * Prints HTML with meta information for the current post (category, tags and permalink).
     *
     * @since Twenty Ten 1.0
     */
    function bhutanese_refugees_posted_in() {
        // Retrieves tag list of current post, separated by commas.
        $tag_list = get_the_tag_list('', ', ');
        if ($tag_list) {
            $posted_in = __('This entry was posted in %1$s and tagged %2$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'bhutanese_refugees');
        } elseif (is_object_in_taxonomy(get_post_type(), 'category')) {
            $posted_in = __('This entry was posted in %1$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'bhutanese_refugees');
        } else {
            $posted_in = __('Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'bhutanese_refugees');
        }
        // Prints the string, replacing the placeholders.
        printf(
                $posted_in, get_the_category_list(', '), $tag_list, get_permalink(), the_title_attribute('echo=0')
        );
    }

endif;

if (!function_exists('bhutanese_refugees_comment')) :

    /**
     * Template for comments and pingbacks.
     *
     * To override this walker in a child theme without modifying the comments template
     * simply create your own bhutanese_refugees_comment(), and that function will be used instead.
     *
     * Used as a callback by wp_list_comments() for displaying the comments.
     *
     * @since Twenty Ten 1.0
     */
    function bhutanese_refugees_comment($comment, $args, $depth) {
        $GLOBALS['comment'] = $comment;
        switch ($comment->comment_type) :
            case '' :
                ?>
                <li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
                    <div id="comment-<?php comment_ID(); ?>">
                        <div class="comment-author vcard">
                <?php echo get_avatar($comment, 40); ?>
                            <?php printf(__('%s <span class="says">says:</span>', 'bhutanese_refugees'), sprintf('<cite class="fn">%s</cite>', get_comment_author_link())); ?>
                        </div><!-- .comment-author .vcard -->
                            <?php if ($comment->comment_approved == '0') : ?>
                            <em class="comment-awaiting-moderation"><?php _e('Your comment is awaiting moderation.', 'bhutanese_refugees'); ?></em>
                            <br />
                <?php endif; ?>

                        <div class="comment-meta commentmetadata"><a href="<?php echo esc_url(get_comment_link($comment->comment_ID)); ?>">
                <?php
                /* translators: 1: date, 2: time */
                printf(__('%1$s at %2$s', 'bhutanese_refugees'), get_comment_date(), get_comment_time());
                ?></a><?php edit_comment_link(__('(Edit)', 'bhutanese_refugees'), ' ');
                ?>
                        </div><!-- .comment-meta .commentmetadata -->

                        <div class="comment-body"><?php comment_text(); ?></div>

                        <div class="reply">
                            <?php comment_reply_link(array_merge($args, array('depth' => $depth, 'max_depth' => $args['max_depth']))); ?>
                        </div><!-- .reply -->
                    </div><!-- #comment-##  -->

                    <?php
                    break;
                case 'pingback' :
                case 'trackback' :
                    ?>
                <li class="post pingback">
                    <p><?php _e('Pingback:', 'bhutanese_refugees'); ?> <?php comment_author_link(); ?><?php edit_comment_link(__('(Edit)', 'bhutanese_refugees'), ' '); ?></p>
                    <?php
                    break;
            endswitch;
        }

    endif;

    if (!function_exists('bhutanese_refugees_feedback')) :

        /**
         * Template for comments and pingbacks.
         *
         * To override this walker in a child theme without modifying the comments template
         * simply create your own bhutanese_refugees_comment(), and that function will be used instead.
         *
         * Used as a callback by wp_list_comments() for displaying the comments.
         *
         * @since Twenty Ten 1.0
         */
        function bhutanese_refugees_feedback($comment, $args, $depth) {
            $commentType = "feedback";
            $GLOBALS['comment'] = $comment;
            switch ($comment->comment_type) :
                case '' :
                    ?>
                <li <?php comment_class('feedback_wrapper'); ?> id="li-comment-<?php comment_ID(); ?>">
                    <div id="comment-<?php comment_ID(); ?>" class="clearfix">
                        <div class="feedback_quote">
                            <img class="" src="<?php echo get_template_directory_uri() ?>/images/quote.jpg" width="59" height="43" alt="quote" />
                        </div>
                        <div class="feedback_content comment-body">
                            <?php if ($comment->comment_approved == '0') : ?>
                                <em class="comment-awaiting-moderation"><?php _e('Your ' . $commentType . ' is awaiting moderation.', 'bhutanese_refugees'); ?></em>
                                <br />
                            <?php endif; ?>
                            
                            <?php comment_text(); ?>
                        </div>
                    </div><!-- #comment-##  -->

                    <?php
                    break;
                case 'pingback' :
                case 'trackback' :
                    ?>
                <li class="post pingback">
                <!--		<p><?php _e('Pingback:', 'bhutanese_refugees'); ?> <?php comment_author_link(); ?><?php edit_comment_link(__('(Edit)', 'bhutanese_refugees'), ' '); ?></p>-->
                    <?php
                    break;
            endswitch;
        }

    endif;

    function wpb_first_and_last_menu_class($items) {
        $items[1]->classes[] = 'first-item';
        $items[count($items)]->classes[] = 'last-item';
        return $items;
    }

    add_filter('wp_nav_menu_objects', 'wpb_first_and_last_menu_class');

    function latest_updated_site() {
        $args = array(
            'post_type' => array('post', 'page'),
            'posts_per_page' => '1',
            'order' => "DESC",
            'orderby' => 'modified'
        );
        query_posts($args);
        $latest_modified_date = "";
        if (have_posts()) {
            while (have_posts()) {
                the_post();
                $latest_modified_date = get_the_modified_date();
            }
        }
        wp_reset_query();
        return $latest_modified_date;
    }

    /* Admin menu */
    add_action('admin_menu', 'bhutanese_refugees_add_page');

    function bhutanese_refugees_add_page() {
        $page0 = add_menu_page('Bhutanese Refugees Option', 'Bhutanese Refugees Option', 'administrator', 'bhutanese-refugees', 'bhutanese_refugees_theme_options_render_page');
        $page1 = add_submenu_page('bhutanese-refugees', "Bhutanese Refugees Setting", "Bhutanese Refugees Setting", 'administrator', 'bhutanese-refugees', 'bhutanese_refugees_theme_options_render_page');
        if (!$page0 || $page1)
            return;
    }

    function bhutanese_refugees_theme_options_render_page() {

        //must check that the user has the required capability 
        if (!current_user_can('manage_options')) {
            wp_die(__('You do not have sufficient permissions to access this page.'));
        }

        // variables for the field and option names 
        $opt_name = BHUTANESE_REFUGEES_OPTION_NAME;
        $hidden_field_name = 'mt_submit_hidden';


        // See if the user has posted us some information
        // If they did, this hidden field will be set to 'Y'
        if (isset($_POST[$hidden_field_name]) && $_POST[$hidden_field_name] == 'Y') {
            // Read their posted value
            unset($_POST[$hidden_field_name]);
            unset($_POST['submit']);
            // Save the posted value in the database
            update_option($opt_name, $_POST);
            // Put an settings updated message on the screen
            ?>
            <div class="updated"><p><strong><?php _e('Settings saved.', 'bhutanese_refugees'); ?></strong></p></div>
            <?php
        }
        // Read in existing option value from database
        $opt_val = get_option($opt_name);

        // Now display the settings editing screen

        echo '<div class="wrap">';

        // header
        // settings form
        define(BHUTANESE_REFUGEES_MENU_NAME, (isset($bhutanese_settings["main_menu"]) && is_numeric($bhutanese_settings["main_menu"])) ? $bhutanese_settings["main_menu"] : "Main Menu");
        define(BHUTANESE_REFUGEES_FOOTER_MENU_NAME, (isset($bhutanese_settings["footer_menu"]) && is_numeric($bhutanese_settings["footer_menu"])) ? $bhutanese_settings["footer_menu"] : "Footer Menu");
        define(BHUTANESE_REFUGEES_HOMEPAGE_ID, (isset($bhutanese_settings["homepageId"]) && is_numeric($bhutanese_settings["homepageId"])) ? $bhutanese_settings["homepageId"] : 10);
        define(BHUTANESE_REFUGEES_NEWS_ID, (isset($bhutanese_settings["newsId"]) && is_numeric($bhutanese_settings["newsId"])) ? $bhutanese_settings["newsId"] : 4);
        define(BHUTANESE_REFUGEES_BLOG_ID, (isset($bhutanese_settings["blogId"]) && is_numeric($bhutanese_settings["blogId"])) ? $bhutanese_settings["blogId"] : 9);
        ?>
        <style>
            .width25{
                width: 25%!important;
            }
        </style>
        <div class="form-wrap">
            <h3><?php __('Bhutanese Refugees Settings', 'bhutanese_refugees') ?></h3>
            <form class="validate" name="frm_bhutanese_refugees" method="post" action="">
                <input type="hidden" name="<?php echo $hidden_field_name; ?>" value="Y">
                <div class="form-field form-required">
                    <label for="facebook_url"><?php _e("Facebook Url", 'bhutanese_refugees'); ?> </label>
                    <input type="text" aria-required="true" size="20" name="facebook_url" id="facebook_url" value="<?php echo @$opt_val["facebook_url"] ?>" class="width25">
                    <p>Facebook Page Url. e.g; http://www.facebook.com/nepal</p>
                </div>
                <div class="form-field form-required">
                    <label for="twitter_url"><?php _e("Twitter Url", 'bhutanese_refugees'); ?> </label>
                    <input type="text" aria-required="true" size="20" name="twitter_url" id="twitter_url" value="<?php echo @$opt_val["twitter_url"] ?>" class="width25">
                    <p>Twitter Page Url. e.g; http://www.twitter.com/nepal</p>
                </div>
                <div class="form-field form-required">
                    <label for="youtube_url"><?php _e("Youtube Url", 'bhutanese_refugees'); ?> </label>
                    <input type="text" aria-required="true" size="20" name="youtube_url" id="youtube_url" value="<?php echo @$opt_val["youtube_url"] ?>" class="width25">
                    <p>Youtube Url. e.g; http://www.youtube.com/nepal</p>
                </div>


                <div class="form-field form-required">
                    <label for="main_menu"><?php _e("Main Menu name", 'bhutanese_refugees'); ?> </label>
                    <input type="text" aria-required="true" size="20" name="main_menu" id="main_menu" value="<?php echo @$opt_val["main_menu"] ?>" class="width25">
                    <p>The name of Main menu that we have created.</p>
                </div>
                <div class="form-field form-required">
                    <label for="footer_menu"><?php _e("Footer Menu name", 'bhutanese_refugees'); ?> </label>
                    <input type="text" aria-required="true" size="20" name="footer_menu" id="footer_menu" value="<?php echo @$opt_val["footer_menu"] ?>" class="width25">
                    <p>The name of Footer menu that we have created.</p>
                </div>
                <div class="form-field form-required">
                    <label for="homepageId"><?php _e("HomePage Id", 'bhutanese_refugees'); ?> </label>
                    <input type="text" aria-required="true" size="20" name="homepageId" id="homepageId" value="<?php echo @$opt_val["homepageId"] ?>" class="width25">
                    <p>ID of a page which is assumed to be our homepage.</p>
                </div>
                <div class="form-field form-required">
                    <label for="newsId"><?php _e("News Category Id", 'bhutanese_refugees'); ?> </label>
                    <input type="text" aria-required="true" size="20" name="newsId" id="newsId" value="<?php echo @$opt_val["newsId"] ?>" class="width25">
                    <p>News Category ID.</p>
                </div>
                <div class="form-field form-required">
                    <label for="blogId"><?php _e("Blog Category Id", 'bhutanese_refugees'); ?> </label>
                    <input type="text" aria-required="true" size="20" name="blogId" id="blogId" value="<?php echo @$opt_val["blogId"] ?>" class="width25">
                    <p>Blog Category ID.</p>
                </div>
                <div class="form-field form-required">
                    <label for="footer_text"><?php _e("Footer Text", 'bhutanese_refugees'); ?> </label>
                    <textarea id="footer_text" name="footer_text" class="width25"><?php echo @$opt_val["footer_text"] ?></textarea> 
                    <p>Footer ext which will be displayed on footer section.</p>
                </div>
                <p class="submit">
                    <input type="submit" value="<?php esc_attr_e('Save Changes') ?>" class="button button-primary" id="submit" name="submit">
                </p>
            </form>
        </div>

        <?php
    }

    /* Admin panel customization */
    add_filter('admin_footer_text', 'my_footer_text');
    add_filter('update_footer', 'my_footer_version', 11);

    function my_footer_text($default) {
        //return $default . ' Also, I am a WordPress banana.';
        return '';
    }

    function my_footer_version($default) {
        //return 'Not ' . $default;
        return '';
    }

    function annointed_admin_bar_remove() {
        global $wp_admin_bar;

        /* Remove their stuff */
        $wp_admin_bar->remove_menu('wp-logo');
    }

    add_action('wp_before_admin_bar_render', 'annointed_admin_bar_remove', 0);

    function search_with_button_submit() {
        ?>
        <div id="my-search">
            <form method="get" class="search_form_visible" action="<?php bloginfo('home'); ?>/">
                <span>   <input class="text_input rnd_corner_small" type="text" value="Enter Text &amp; Click to Search" name="s" id="s" onfocus="if (this.value == 'Enter Text &amp; Click to Search') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Enter Text &amp; Click to Search';}" />
                </span><span>  <input type="submit" class="my-search rnd_corner_small" id="searchsubmit" value="ok" />
                </span>
            </form>
        </div>
        <?php
    }

    add_action('thesis_hook_header', 'search_with_button_submit');
    